Paranoia.Config = {};
Paranoia.Config.ConfigCopy = {};

local Paranoia = Paranoia;

--Dialogs
StaticPopupDialogs["PARANOIARUIFRAMES"] = {
  text = PARANOIA_RELOADPROMPTTEXT,
  button1 = YES,
  button2 = NO,
  OnAccept = function() Paranoia.Config.DoResetFrames() end,
  timeout = 0,
  whileDead = 1,
  hideOnEscape = 0,
  showAlert = 1,
};
  
StaticPopupDialogs["PARANOIARUICONFIG"] = {
  text = PARANOIA_DEFAULTPROMPTTEXT,
  button1 = YES,
  button2 = NO,
  OnAccept = function() Paranoia.Config.DoResetSettings() end,
  timeout = 0,
  whileDead = 1,
  hideOnEscape = 0,
  showAlert = 1,
};

StaticPopupDialogs["PARANOIAFIRST"] = {
  text = string.format(PARANOIA_FIRSTRUNPROMPTTEXT, GetZoneText()),
  button1 = YES,
  button2 = NO,
  OnAccept = function() Paranoia.Config.ShowConfig() end,
  timeout = 0,
  whileDead = 1,
  hideOnEscape = 0,
  showAlert = 1,
};

StaticPopupDialogs["PARANOIAKILLED"] = {
    text = "Paranoia: Shouldn't see this (non-initialized text)",
    button1 = YES,
    button2 = NO,
    timeout = 10,
    whileDead = 1,
    hideOnEscape = 1,
    showAlert = 1,
  };

function Paranoia.Config.VerifyConfig(defaults)
  if defaults == nil then defaults = false; end
  
  if (ParanoiaData.Config.Enabled == nil) then ParanoiaData.Config.Enabled = true; end
  if (ParanoiaData.Config.DisableInBG == nil) then ParanoiaData.Config.DisableInBG = true; end
  if (ParanoiaData.Config.DisableInArena == nil) then ParanoiaData.Config.DisableInArena = true; end
  if (ParanoiaData.Config.DisableInSanctuaries == nil) then ParanoiaData.Config.DisableInSanctuaries = true; end
  if (ParanoiaData.Config.DisableInFFA == nil) then ParanoiaData.Config.DisableInFFA = true; end
  if (ParanoiaData.Config.HideList == nil) then ParanoiaData.Config.HideList = false; end
  if (ParanoiaData.Config.Locked == nil) then ParanoiaData.Config.Locked = false; end
  if (ParanoiaData.Config.SoundEnabled == nil) then ParanoiaData.Config.SoundEnabled = true; end
  if (ParanoiaData.Config.FadeInactive == nil) then ParanoiaData.Config.FadeInactive = true; end
  if (ParanoiaData.Config.BorderOpacity == nil) then ParanoiaData.Config.BorderOpacity = 1; end
  if (ParanoiaData.Config.FrameOpacity == nil) then ParanoiaData.Config.FrameOpacity = 1; end
  if (ParanoiaData.Config.WarnPopupEnabled == nil) then ParanoiaData.Config.WarnPopupEnabled = true; end
  if (ParanoiaData.Config.WarnPopupFadeDuration == nil) then ParanoiaData.Config.WarnPopupFadeDuration = 2; end
  if (ParanoiaData.Config.WarnPopupDisplayDuration == nil) then ParanoiaData.Config.WarnPopupDisplayDuration = 3; end
  if (ParanoiaData.Config.WarnPopupAlpha == nil) then ParanoiaData.Config.WarnPopupAlpha = 1; end
  if (ParanoiaData.Config.ListTimeout == nil) then ParanoiaData.Config.ListTimeout = 60; end
  if (ParanoiaData.Config.WarnTimeout == nil) then ParanoiaData.Config.WarnTimeout = 60; end
  if (ParanoiaData.Config.MaxHostiles == nil) then ParanoiaData.Config.MaxHostiles = 15; end
  if (ParanoiaData.Config.CustomAnnounce == nil) then ParanoiaData.Config.CustomAnnounce = PARANOIA_DEFAULTMSG; end
  if (ParanoiaData.Config.ListScale == nil) then ParanoiaData.Config.ListScale = 1.0; end
  if (ParanoiaData.Config.CommEnabled == nil) then ParanoiaData.Config.CommEnabled = true; end
  if (ParanoiaData.Config.CommParty == nil) then ParanoiaData.Config.CommParty = true; end
  if (ParanoiaData.Config.CommGuild == nil) then ParanoiaData.Config.CommGuild = true; end
  if (ParanoiaData.Config.MinimapPos == nil) then ParanoiaData.Config.MinimapPos = -62; end
  if (ParanoiaData.Config.HideMinimap == nil) then ParanoiaData.Config.HideMinimap = false; end
end

function Paranoia.Config.ResetSettings()
  StaticPopup_Show("PARANOIARUICONFIG");
end

function Paranoia.Config.DoResetSettings()
  ParanoiaData.Config = {};
  ReloadUI();
end

function Paranoia.Config.ResetFrames()
  StaticPopup_Show("PARANOIARUIFRAMES");
end

function Paranoia.Config.DoResetFrames()
  Paranoia_EnemyList:SetUserPlaced(false);
  Paranoia_EnemyListCombat:SetUserPlaced(false);
  Paranoia_WarnPopup:SetUserPlaced(false);
  Paranoia_KoSPopup:SetUserPlaced(false);
  ReloadUI();
end

function Paranoia.Config.InitBlizzOptions()
  local frame = CreateFrame("FRAME", nil);
  frame:SetScript("OnShow",function() Paranoia.Config.ShowConfig() end);
  frame.name = "Paranoia";
  frame.okay = function() Paranoia.Config.Okay() end
  frame.cancel = function() Paranoia.Config.Cancel() end
  InterfaceOptions_AddCategory(frame);
  
  ParanoiaConf_Frame.name = PARANOIA_TABGENERAL;
  ParanoiaConf_Frame.parent = "Paranoia";
  ParanoiaConf_Frame.default = function() Paranoia.Config.ResetSettings() end
  InterfaceOptions_AddCategory(ParanoiaConf_Frame);
  
  ParanoiaConf_Appearance.name = PARANOIA_TABAPPEARANCE;
  ParanoiaConf_Appearance.parent = "Paranoia";
  ParanoiaConf_Appearance.default = function() Paranoia.Config.ResetSettings() end
  InterfaceOptions_AddCategory(ParanoiaConf_Appearance);
  
  ParanoiaConf_AlertPopup.name = PARANOIA_TABALERTPOPUP;
  ParanoiaConf_AlertPopup.parent = "Paranoia";
  ParanoiaConf_AlertPopup.default = function() Paranoia.Config.ResetSettings() end
  InterfaceOptions_AddCategory(ParanoiaConf_AlertPopup);
  
  ParanoiaConf_Announce.name = PARANOIA_TABANNOUNCE;
  ParanoiaConf_Announce.parent = "Paranoia";
  ParanoiaConf_Announce.default = function() Paranoia.Config.ResetSettings() end
  InterfaceOptions_AddCategory(ParanoiaConf_Announce);
  
  ParanoiaConf_Communication.name = PARANOIA_TABCOMM;
  ParanoiaConf_Communication.parent = "Paranoia";
  ParanoiaConf_Communication.default = function() Paranoia.Config.ResetSettings() end
  InterfaceOptions_AddCategory(ParanoiaConf_Communication);
end

--[[
Config UI Functions
]]--

function Paranoia.Config.ShowConfig()
  if InCombatLockdown() then Paranoia:Msg("Paranoia cannot be configured while in combat!"); return; end
  InterfaceOptionsFrame_OpenToCategory(PARANOIA_TABGENERAL);
  Paranoia.Config.IsConfigOpen = true;
  Paranoia.Config.ConfigCopy = {};
  for k,v in pairs(ParanoiaData.Config) do Paranoia.Config.ConfigCopy[k] = v; end
end

function Paranoia.Config.CheckboxSet(self, option)
  ParanoiaData.Config[option] = Paranoia:ToBoolean(self:GetChecked());
end

function Paranoia.Config.CheckboxGet(self, option)
  self:SetChecked(Paranoia:ToBoolean(ParanoiaData.Config[option]));
end

function Paranoia.Config.SliderSet(self, option)
  ParanoiaData.Config[option] = self:GetValue();
end

function Paranoia.Config.SliderGet(self, option)
  self:SetValue(ParanoiaData.Config[option]);
end

function Paranoia.Config.LockCheckboxSet(self)
  if Paranoia:ToBoolean(self:GetChecked()) then
    Paranoia.List.SetLock(true);
    ParanoiaData.Config["Locked"] = true;
  else
    Paranoia.List.SetLock(false);
    ParanoiaData.Config["Locked"] = false;
  end
end

function Paranoia.Config.WarnFrameShow()
  Paranoia_WarnPopup:Clear();
  Paranoia_WarnPopup:SetBackdrop({bgFile = "Interface/DialogFrame/UI-DialogBox-Background"});
  Paranoia_WarnPopup:EnableMouse(true);
  Paranoia_WarnPopup:Show();
  Paranoia_WarnPopup:SetTimeVisible(500);
  Paranoia_WarnPopup:AddMessage(PARANOIA_ALERTMOVEABLE, 0, 1, 0)
end

function Paranoia.Config.WarnFrameHide()
  Paranoia_WarnPopup:Clear();
  Paranoia_WarnPopup:SetBackdrop(nil);
  Paranoia_WarnPopup:EnableMouse(false);
  Paranoia_WarnPopup:SetTimeVisible(ParanoiaData.Config.WarnPopupDisplayDuration);
  Paranoia_WarnPopup:SetFadeDuration(ParanoiaData.Config.WarnPopupFadeDuration);
end

function Paranoia.Config.Okay()
  if InCombatLockdown() then Paranoia:Msg("Paranoia cannot be configured while in combat!"); return; end
  Paranoia:Msg("Your settings have been saved.")
  Paranoia.Config.IsConfigOpen = false;
  Paranoia.Config.WarnFrameHide();
  Paranoia:UpdateMinimapVisibility();
end

function Paranoia.Config.Cancel()
  if InCombatLockdown() then Paranoia:Msg("Paranoia cannot be configured while in combat!"); return; end
  Paranoia:Msg("Changes to settings were discarded.")
  Paranoia.Config.IsConfigOpen = false;
  Paranoia.Config.WarnFrameHide();
  for k,v in pairs(Paranoia.Config.ConfigCopy) do ParanoiaData.Config[k] = v; end
  Paranoia.List.SetLock(ParanoiaData.Config["Locked"]);
  Paranoia_EnemyList:SetScale(ParanoiaData.Config["ListScale"]);
  Paranoia_EnemyListCombat:SetScale(ParanoiaData.Config["ListScale"]);
  if Paranoia_EnemyList:GetAlpha() ~= 0 then Paranoia_EnemyList:SetAlpha(ParanoiaData.Config["FrameOpacity"]); end
  if Paranoia_EnemyListCombat:GetAlpha() ~= 0 then Paranoia_EnemyListCombat:SetAlpha(ParanoiaData.Config["FrameOpacity"]); end
  Paranoia_EnemyList:SetBackdropBorderColor(255, 255, 255, ParanoiaData.Config["BorderOpacity"]);
  Paranoia_EnemyListCombat:SetBackdropBorderColor(255, 255, 255, ParanoiaData.Config["BorderOpacity"]);
  Paranoia_WarnPopup:SetAlpha(ParanoiaData.Config["WarnPopupAlpha"]);
  Paranoia:UpdateMinimapVisibility();
end
